# -*- coding: utf-8 -*-
import sqlite3

class Settings():
    settings = {}
    settings["tables"] = [ 
                          [ "Widerstände", 
                           ["UID", "Wert", "Einheit", "Material", "Leistung", "Bauform", "Menge", "Preis", "Reichelt", "Notizen"],
                           [1,3,4,5,6,7,8,9],
                           {"UID": "INTEGER PRIMARY KEY NOT NULL UNIQUE", "Wert": "INTEGER", "Einheit": "TEXT DEFAULT Ohm", "Material" : "TEXT", "Leistung": "REAL", "Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"},
                           {"UID": "QLineEdit", "Wert": "INTEGER", "Einheit": "TEXT DEFAULT Ohm", "Material" : "TEXT", "Leistung": "REAL", "Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"} 
                          ],
                          [ "Kondensatoren",
                           ["UID", "Kapazität", "Einheit", "Typ", "Spannungsfestigkeit", "Rastermaß", "Bauform", "Menge", "Preis", "Reichelt", "Notizen"],
                           [1, 3, 4, 5, 6, 7, 8, 9],
                           {"UID": "INTEGER PRIMARY KEY NOT NULL UNIQUE", "Kapazität": "REAL", "Einheit": "TEXT", "Typ": "TEXT", "Spannungsfestigkeit": "INT", "Rastermaß": "REAL", "Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
                          ],
                          [ "Dioden", 
                           ["UID", "Bezeichnung", "Typ", "Spannung", "Stromstärke", "Bauform", "Menge", "Preis", "Reichelt","Notizen"], 
                           [2,8,9],
                           {"UID": "INTEGER PRIMARY KEY  NOT NULL  UNIQUE" , "Bezeichnung": "TEXT", "Typ": "TEXT", "Spannung": "REAL", "Stromstärke": "TEXT", "Bauform": "TEXT", "Menge": "INTEGER DEFAULT 0", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
                          ],
                          [ "Transistoren",
                           ["UID", "Typennummer", "Typ", "Spannungsfestigkeit", "Stromfestigkeit","Bauform", "Menge", "Preis", "Reichelt", "Notizen"],
                           [1,2,3,4],
                           {"UID": "INT", "Typennummer": "TEXT", "Typ": "TEXT", "Spannungsfestigkeit": "INT", "Stromfestigkeit": "INT","Bauform": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
                          ],
                          [ "IC",
                           ["UID", "Typennummer", "Typ", "Bauform" ,"Menge", "Preis", "Reichelt", "Notizen"],
                           [1,2,3],
                           {"UID":"INT", "Typennummer":"TEXT", "Typ":"TEXT", "Bauform":"TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
                          ],
#                          [ "",
#                           [,"Menge", "Preis", "Reichelt", "Notizen"],
#                           [],
#                           {, "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}
#                          ],
                          [ "Taktgeneratoren",
                           ["UID", "Frequenz", "Einheit", "Typ", "Menge", "Preis", "Reichelt", "Notizen"],
                           [1, 3, 6],
                           {"UID": "INTEGER PRIMARY KEY  NOT NULL  UNIQUE", "Frequenz": "REAL", "Einheit": "TEXT", "Typ": "TEXT", "Menge": "INT", "Preis": "REAL", "Reichelt": "TEXT", "Notizen": "TEXT"}                            
                          ] 
                         ]
    def __init__(self):
        self.settingsConnection = sqlite3.connect("databases/settings.sqlite")
        self.settingsCursor = self.settingsConnection.cursor() 
        try:
            self.settingsCursor.execute('SELECT * FROM "main"."settings"')
            for row in self.settingsCursor:
                self.settings[row[0]] = row[1]         
        except Exception:
            try:
                self.settingsCursor.execute('CREATE TABLE "main"."settings" ("key" TEXT, "value" BLOB)')
            except:
                pass
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowHeight", "768")')
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowWidth", "1024")')  
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowPositionX", "0")')
            self.settingsCursor.execute('INSERT INTO "main"."settings" VALUES ("windowPositionY", "0")')
            self.settingsConnection.commit()
            self.__init__()
            
    def __del__(self):
        self.settingsConnection.commit()
        self.settingsConnection.close()